package com.algorithm.offer.linkedtable;

/**
 * @Author: huangzhigao
 * @Date: 2021/11/8 21:48
 */
public class JZ25 {
    public ListNode Merge(ListNode list1, ListNode list2) {
        if (null == list1) {
            return list2;
        }
        if (null == list2) {
            return list1;
        }

        ListNode res = null;
        ListNode cur = null;
        while (list1 != null && list2 != null) {
            if (list1.val >= list2.val) {
                if (res == null) {
                    res = cur = list1;
                } else {
                    cur.next = list1;
                    cur = cur.next;
                }
                list1 = list1.next;
            } else {
                if (res == null) {
                    res = cur = list2;
                } else {
                    cur.next = list2;
                    cur = cur.next;
                }
                list2 = list2.next;
            }
        }

        if (list1 == null) {
            cur.next = list2;
        }else {
            cur.next = list1;
        }
        return res;

    }
}
